<?php
class User extends AppModel {
	var $name = 'User';
	var $belongsTo = array('Group');
	var $actsAs = array('Acl' => array('requester'), 'ExtendAssociations');
	var $hasAndBelongsToMany = array(
        'Role' =>
            array(
	            'className'              => 'Role',
	            'joinTable'              => 'roles_users',
	            'foreignKey'             => 'user_id',
                'associationForeignKey'  => 'role_id',
                'unique'                 => true,
                'conditions'             => '',
                'fields'                 => '',
                'order'                  => '',
                'limit'                  => '',
                'offset'                 => '',
                'finderQuery'            => '',
                'deleteQuery'            => '',
                'insertQuery'            => ''
            ),
		'Survey' =>
			array(
				'className'              => 'Survey',
	            'joinTable'              => 'surveys_users',
	            'foreignKey'             => 'user_id',
                'associationForeignKey'  => 'survey_id',
                'unique'                 => true,
                'conditions'             => '',
                'fields'                 => '',
                'order'                  => '',
                'limit'                  => '',
                'offset'                 => '',
                'finderQuery'            => '',
                'deleteQuery'            => '',
                'insertQuery'            => ''
			),
		'Audiencelist' =>
			array(
				'className'              => 'Audiencelist',
	            'joinTable'              => 'audiencelists_users',
	            'foreignKey'             => 'user_id',
                'associationForeignKey'  => 'audiencelist_id',
                'unique'                 => true,
                'conditions'             => '',
                'fields'                 => '',
                'order'                  => '',
                'limit'                  => '',
                'offset'                 => '',
                'finderQuery'            => '',
                'deleteQuery'            => '',
                'insertQuery'            => ''
			)	
    );

	var $hasMany = 'Audiencelist';

	var $validate = array(
		'username' => array(
			'username-1' => array(
            'rule' => 'notEmpty',  
            'message' => 'Please enter a username'
	         ),
	        'username-2' => array(
	            'rule' => 'isUnique',  
	            'message' => 'This username has already been taken'
	        )
		),
		'firstname' => array(
			'rule' => 'alphaNumeric',
			'required' => true,
			'allowEmpty' => false,
			'message' => 'Please enter a first name'
		),
		'lastname' => array(
			'rule' => 'alphaNumeric',
			'required' => true,
			'allowEmpty' => false,
			'message' => 'Please enter a last name'
		),
		'email' => array(
			'email-1' => array(
            'rule' => 'notEmpty',  
            'message' => 'Please enter an email'
	         ),
	        'email-2' => array(
	            'rule' => 'email',  
	            'message' => 'Please enter a correctly formatted email'
	        )
		),
		'password' => array(
			'rule' => 'alphaNumeric',
			'required' => true,
			'allowEmpty' => false,
			'message' => 'Please enter a password'
		)
	);
	
	function parentNode() {
	    if (!$this->id && empty($this->data)) {
	        return null;
	    }
	    $data = $this->data;
	    if (empty($this->data)) {
	        $data = $this->read();
	    }
	    if (!$data['User']['group_id']) {
	        return null;
	    } else {
	        return array('Group' => array('id' => $data['User']['group_id']));
	    }
	}
}
?>
